export default {
    // 异步方法: 函数 ->  注意actions中不能修改数据, 如果需要修改数据,必须先在mutations中定义同步方法修改数据,在调用mutations中的方法
    // 函数中的第一参数: context -> 和store实例有相同属性和方法的新对象   {state,getters,commit,dispatch}
    //  函数中的第二参数: payload(载荷)  actions方法被调用时可以接收一个实际参数被形参接收 

    // context = {state:store.state,getters:store.getters, commit:store.commit, dispatch:store.dispatch}

    // addA_Async(context, payload) {
    //   console.log("addA_Async触发了:context", context);  //{state,getters,commit,dispatch}
    //   console.log("addA_Async触发了:payload", payload);

    //   return 100;   // 
    // },
    addA_Async(context, payload) {
      console.log("addA_Async触发了:context", context);  //{state,getters,commit,dispatch}
      console.log("addA_Async触发了:payload", payload);

      return new Promise(function (resolve) {  // 可以知道异步操作是否结束
        setTimeout(() => {

          context.commit("addA");
          resolve();
        }, 1000)
      })
    },
    addB_Async(context, payload) {
      console.log("addB_Async触发了:context", context);  //{state,getters,commit,dispatch}
      console.log("addB_Async触发了:payload", payload);
      return new Promise(function (resolve) {
        setTimeout(() => {
          context.commit("addB");
          resolve();
        }, 1000)
      })
    },
    addAN_Async(context, payload) {
      console.log("addAN_Async触发了:context", context);  //{state,getters,commit,dispatch}
      console.log("addAN_Async触发了:payload", payload);
      setTimeout(() => {
        // 如何调用mutations中的方法!!!!!
        context.commit("addAN", payload);
      }, 1000)
    },
    addBoth_Async(context, payload) {
      console.log("addBoth_Async触发了:context", context);  //{state,getters,commit,dispatch}
      console.log("addBoth_Async触发了:payload", payload);
      // 在actions方法中调用另一个异步方法
      context.dispatch("addA_Async");
      context.dispatch("addB_Async");
    },
    test: async function (context, payload) {
      // context = {state,getters,commit,dispatch}

      let { state, getters, commit, dispatch } = context;
      state
      getters
      commit
      dispatch
      payload


      // var res = commit("addA");
      // console.log("同步方法的返回值",res);  // undefined

      var res = dispatch("addA_Async");  // 返回Promise实例  1s变为fulfilled 
      
      // 链式接收 -> 麻烦
      res.then(data => {
        console.log("addA_Async执行完毕", data);
        return  dispatch("addB_Async");
      }).then(data => {
        console.log("addB_Async执行完毕", data);
      }).catch(err => {
        console.log("addA_Async执行过程出错", err);
      })



      // async await 接收 -> 
      // var res = await dispatch("addA_Async");  // 返回Promise实例  => 默认pending, 1s变为fulfilled 
      // await dispatch("addB_Async"); 


    }
  }